package com.kadron.creator;

import com.kadron.comment.KCommentModel;
import com.kadron.dao.KCommentDAO;
import com.kadron.dao.KImageDAO;
import com.kadron.dao.KUserImageDAO;
import com.kadron.datamodel.KModelCreator;
import com.kadron.entity.Comment;
import com.kadron.entity.KItemImage;

public class KCommentModelCreator implements KModelCreator {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5431750255930389850L;

	private KUserImageDAO userImageDAO;
	private KImageDAO imageDAO;
	private KCommentDAO commentDAO;
	
	public KCommentModelCreator(KUserImageDAO userImageDAO, KImageDAO imageDAO,
			KCommentDAO commentDAO) {
		super();
		this.userImageDAO = userImageDAO;
		this.imageDAO = imageDAO;
		this.commentDAO = commentDAO;
	}

	@Override
	public KCommentModel create(long commentId) {
		Comment comment = (Comment) commentDAO.find(commentId, Comment.class);
		String thumb = "";
		String name = comment.getName();
		Long userId = null;
		if (comment.getUser() != null) {
			name = comment.getUser().getUsername();
			userId = comment.getUser().getId();
			thumb = userImageDAO.findThumb(comment.getUser().getId());
		}
		KCommentModel model = new KCommentModel(commentId, userId, comment.getComment(), thumb, name, comment.getCreated(), comment.isApproved());
		model.setType(comment.getType());
		model.setIpAddress(comment.getIpAddress());
		if (comment.getItem() != null) {
			KItemImage img = imageDAO.getMainItemImage(comment.getItem().getId());
			model.setItemId(comment.getItem().getId());
			model.setItemImg(img);			
		}
		return model;
	}
	
}
